package com.yk.faceapplication.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.allinpay.xed.R;
import com.android.volley.VolleyError;
import com.yk.faceapplication.callback.RequestCallback;
import com.yk.faceapplication.entity.Member;
import com.yk.faceapplication.http.RequestUrl;
import com.yk.faceapplication.util.ActivityUtil;
import com.yk.faceapplication.util.RequestUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yk_2018/1/12_15:02
 */

public class LoginActivity extends BaseActivity {
    private Button loginBtn;
    private EditText phoneET, pwdET;
    private TextView registerTV,rePwdTV;
    private final int FACE_REQUEST = 1000;
    private ImageView eyeIV;
    private boolean eyeIsOpen = false;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setTitle("登录");
        loginBtn = (Button) findViewById(R.id.login_btn);
        phoneET = (EditText) findViewById(R.id.phone_et);
        pwdET = (EditText) findViewById(R.id.pwd_et);
        loginBtn.setOnClickListener(this);
        eyeIV = (ImageView) findViewById(R.id.pwd_eye_img);
        eyeIV.setOnClickListener(this);
        registerTV = (TextView) findViewById(R.id.regiter_tv);
        rePwdTV = (TextView) findViewById(R.id.re_pwd_tv);
        registerTV.setOnClickListener(this);
        rePwdTV.setOnClickListener(this);
        pwdET.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 输入前的监听

            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听
                checkInputInfo();
            }
        });
        phoneET.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 输入前的监听

            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听
                checkInputInfo();
            }
        });
        checkInputInfo();
    }

    private void checkInputInfo(){
        if(ActivityUtil.editIsNull(phoneET) || ActivityUtil.editIsNull(pwdET)){
            loginBtn.setBackgroundColor(getResources().getColor(R.color.bg_gray));
            loginBtn.setEnabled(false);
        }else{
            loginBtn.setBackgroundColor(getResources().getColor(R.color.textRegisterFile));
            loginBtn.setEnabled(true);
        }
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.login_btn:
                login();
                break;
            case R.id.regiter_tv:
                Intent intent = new Intent(this,RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.re_pwd_tv:
                intent = new Intent(this,RePwdActivity.class);
                startActivity(intent);

//                openLivenessActivity(this);
                break;
            case R.id.pwd_eye_img:
                if(eyeIsOpen){
                    eyeIV.setImageResource(R.drawable.pwd_close_icon);
                    pwdET.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    pwdET.setSelection(pwdET.getText().toString().length());
                }else{
                    eyeIV.setImageResource(R.drawable.pwd_close_open);
                    pwdET.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    pwdET.setSelection(pwdET.getText().toString().length());
                }
                eyeIsOpen = !eyeIsOpen;
                break;
        }
    }


    private void login() {
//        if (!ActivityUtil.isMobileNO(phoneET.getText().toString())) {
//            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
//            return;
//        }
        if (ActivityUtil.editIsNull(phoneET)) {
            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (ActivityUtil.editIsNull(pwdET)) {
            Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show();
            return;
        }

//        final Response.Listener<JSONObject> sListener = new Response.Listener<JSONObject>() {
//            @Override
//            public void onResponse(JSONObject jsonObject) {
//                LogDebugger.info("success:" + jsonObject.toString());
//                try {
//                    if("000000".equals(jsonObject.getString("code"))){
//                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//                        mApplication.setToken(jsonObject.getString("token"));
//                        mApplication.setMemberId(jsonObject.getString("tokenMemberId"));
//                        editor.putString("phone",phoneET.getText().toString()).commit();
//                        editor.putString("pwd",pwdET.getText().toString()).commit();
//                        editor.putString("token",jsonObject.getString("token")).commit();
//                        Member member = JSON.parseObject(jsonObject.getString("data"),Member.class);
//                        mApplication.setMember(member);
//                        setResult(RESULT_OK);
//                        finishAct();
//                    }else{
//                        Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
//                    }
//
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        };
//        final Response.ErrorListener eListener = new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError volleyError) {
//                volleyError.printStackTrace();
//                LogDebugger.info("fail:" + volleyError.getMessage());
//                Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
//            }
//        };
//
//        try {
//            JSONObject jObj = new JSONObject();
//            JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST,
//                    RequestUrl.LOGIN + "name=" + phoneET.getText().toString() + "&password=" + pwdET.getText().toString(), jObj,
//                    sListener, eListener) {
//
//            };
//            ((MyApplication) this.getApplicationContext()).getrQueue().add(req);
//        } catch (Exception e) {
//
//        }




        Map<String, String> form = new HashMap<String, String>();

        RequestCallback callback = new RequestCallback() {
            @Override
            public void success(JSONObject jsonObject) {
                try {
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    mApplication.setToken(jsonObject.getString("token"));
                    mApplication.setMemberId(jsonObject.getString("tokenMemberId"));
                    editor.putString("phone",phoneET.getText().toString()).commit();
                    editor.putString("pwd",pwdET.getText().toString()).commit();
                    editor.putString("token",jsonObject.getString("token")).commit();
                    Member member = JSON.parseObject(jsonObject.getString("data"),Member.class);
                    mApplication.setMember(member);
                    mApplication.setHomePageGetMember(true);
                    setResult(RESULT_OK);
                    finishAct();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
            }
        };
        RequestUtil.getRequest(this, RequestUrl.LOGIN + "name=" + phoneET.getText().toString() + "&password=" + pwdET.getText().toString(), callback);
    }
}
